/*
 * @authors :Bin Mei
 * @date    :2017-8-04
 * @description：票交汇M站 首页
 */

import React, { Component } from 'react';
import {bindActionCreators} from "redux";
import {connect} from "react-redux";
import {Link} from "react-router";
import classnames from 'classnames';
import actions from "src/store/actions";
import {Svg,Empty} from "src/components/common";
import {  Cell ,Scroll,Button} from 'pjh-mobile-ui';
// import pjhHistory from 'src/utils/pjhHistory';

import './Notice.scss';


class Notice extends Component{
	constructor(props){
		super(props);
		this.state = {
			isShow: false, 
			openUp: false,
			initFlag:false
		};
		this.flag = false;
		this.pageNum = 1;
		this.getData = this.getData.bind(this);
	}
	componentDidMount() {

	}
	getData( type){
		if(this.flag){
			return false;
		};
		this.flag = true;
		if(type == 'down') {
			this.pageNum = 1;
		}
		return new Promise((resolve, reject)=>{
			return this.props.ACTIONS.notice_list({
				bizContent: {
					pageSize: 20,
					pageNum: type == 'down' ? this.pageNum : ++this.pageNum, // 下拉时加载第一页
				},
				success:res=>{
					this.flag = false;
					// compelent&&compelent();
					if(res.code =="0000"){
						this.setState({
							openUp: res.data.totalPage > this.pageNum
						});
						if(!this.state.initFlag){
							this.setState({initFlag:true})
						}
					}
					resolve();
				},error:err=>{
					this.flag = false;
					// compelent&&compelent()
					reject();
				}
			});
		});
	}
	render(){
		let {openUp,initFlag}=this.state;
		let {_noticeList}=this.props;
		return (
			<div className="n-notice">
				<Scroll openDown={true} openUp={openUp} downFetch={(compelent)=>this.getData('down')}  upFetch={(compelent)=>this.getData()} allowScroll={false}>
					{!initFlag?(<Empty type='loading' />):(
						_noticeList.length>0?(
						<ul className="list">
							{
							_noticeList.map((k,v)=>{
								return (
									<li className="item-row" key={v}>
										<Link className="ds-box" to={{pathname:"/app/notice/detail",query:{articleId:k.articleId}}}>
											<p className="cm-flex">{k.articleName}</p>
											<p className="date">{k.date}</p>
										</Link>
									</li>
								);
							})
							}
						</ul>
						):(
							<Empty />
						)
					)}
				</Scroll>
			</div>
		);
	}
};


let mapStateToProps=(state)=>{
	return {
		_noticeList :state.notice.list
	};
}; 

let mapDispatchToProps=(dispatch)=>{
	return {
		ACTIONS:bindActionCreators(actions,dispatch)
	};
};
export default connect(mapStateToProps,mapDispatchToProps)(Notice);

